package com.mhwx.mhlive.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.Toast;

import com.cnlive.libs.util.chat.ChatUtil;
import com.cnlive.libs.util.chat.base.IChat;
import com.mhwx.mhlive.R;
import com.mhwx.mhlive.ui.adapter.TabAdapter;
import com.mhwx.mhlive.ui.base.BaseActivity;
import com.mhwx.mhlive.ui.fragment.MediacChatFragment;
import com.mhwx.mhlive.ui.fragment.MediacLiveFragment;
import com.mhwx.mhlive.util.DimenUtil;
import com.mhwx.mhlive.util.TablayouUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static android.content.ContentValues.TAG;

/**
 * 类功能描述
 *医疗类直播详情
 * @author Guol
 * @version 1.0
 * @copyright 北京明华维新信息技术有限公司
 * @time 2017/5/5
 */
public class MediaclDatails extends BaseActivity {

    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;

    @BindView(R.id.viewpager)
    ViewPager viewpager;

    private TabAdapter adapter;

    public static final String[] tabTitle = new String[]{"直播间", "聊天室"};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_medical);
        ChatUtil.connect(connectListener);
        initFragment();
    }

    private void  initFragment(){

        List<Fragment> fragments = new ArrayList<>();

        for (int i = 0; i < tabTitle.length; i++) {
            if (tabTitle[i].equals("聊天室")){
                fragments.add(MediacChatFragment.newInstance(i));
            } else{
                fragments.add(MediacLiveFragment.newInstance(i + 1));
            }
        }

        adapter = new TabAdapter(getSupportFragmentManager(), fragments,tabTitle);

        viewpager.setOffscreenPageLimit(tabTitle.length );
        //给ViewPager设置适配器
        viewpager.setAdapter(adapter);
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(viewpager);
        //设置可以滑动
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        TablayouUtil.setIndicator(this, mTabLayout, (int) DimenUtil.dp2px(this,38), (int) DimenUtil.dp2px(this,38));

    }
    private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
        @Override
        public void onTokenIncorrect() {
            Log.e(TAG, "ConnectListener TokenIncorrect");
            Toast.makeText(MediaclDatails.this, "TokenIncorrect", Toast.LENGTH_SHORT).show();
            finish();

        }

        @Override
        public void onSuccess(String s) {
            Log.e(TAG, "ConnectListener 连接监听" + s);
            Toast.makeText(MediaclDatails.this, s, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(int i, String s) {
            Log.e(TAG, "ConnectListener errorCode : " + i + " : errorMessage : " + s);
            finish();
        }
    };
}
